package com.qcby.controller;

import com.qcby.Constants;
import com.qcby.model.dto.*;
import com.qcby.model.resp.RespEntity;
import com.qcby.query.MovieQuery;
import com.qcby.service.MovieCommentService;
import com.qcby.service.MovieService;
import com.qcby.service.MovieTypeService;
import com.qcby.util.Page;
import com.qcby.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieTypeService movieTypeService;
    @Autowired
    private MovieCommentService movieCommentService;


    /**
     * 根据用户传过来的值进行搜索
     *
     * @param movieQueryDto
     * @param
     * @return condition为排序条件
     */

    @RequestMapping("/searchMovie")
    private RespEntity<MovieVo> searchMovie(@RequestBody MovieQueryDto movieQueryDto) {
        if (!movieQueryDto.isValid()) {
            MovieQuery mq = movieQueryDto.getMovieQuery();
            String condition = movieQueryDto.getCondition();
            if (mq.getPageNo() == null) {
                mq.setPageNo(1);
            }

            switch (condition) {
                case "0":// 热门
                    break;
                case "1":// 票房
                    break;
                case "2":// 评分
                    break;
            }
            Page<MovieShowVo> page = movieService.selectByCondition(mq, condition);
            System.out.println(page);
            List<MovieShowVo> movieShowVoList = page.getList();
            for (MovieShowVo movieShowVo : movieShowVoList) {
                movieShowVo.setAvgMovieScore(movieShowVo.getAvgMovieScore().setScale(2, RoundingMode.HALF_UP));
            }
            return RespEntity.successCustomize(Constants.FLAG_200, "查询电影成功！", new MovieVo(page, mq));
        }
        throw new IllegalArgumentException("查询电影的参数存在空值！");
    }

    @PostMapping("/searchByMName")
    private RespEntity<Page<MovieShowVo>> searchByMName(@RequestBody MovieQueryMNameDto movieQueryMNameDto) throws IOException {

        System.out.println(movieQueryMNameDto);
        String mName = movieQueryMNameDto.getMName();
        Integer pageNo = movieQueryMNameDto.getPageNo();
        Integer pageSize = movieQueryMNameDto.getPageSize();
        if (pageNo == null || pageNo <= 0) {
            pageNo = 1;
        }
        if (!StringUtils.isEmpty(mName)) {
            Page<MovieShowVo> page = movieService.getByMName(mName, pageNo, pageSize);
            System.out.println(page);

            return RespEntity.successCustomize(Constants.FLAG_200, "根据电影名称模糊查询电影成功！", page);
        }
        throw new IllegalArgumentException("电影名称为空！");
    }

    @RequestMapping("/getByMid")
    private RespEntity<MovieDetailVo> getByMid(@RequestParam Integer mid) {
        if (mid != null) {
            MovieShowVo movieShowVo = movieService.getCommentByMid(mid);
            System.out.println(">>> movieShowVo:" + movieShowVo);
            List<MovieActorVo> movieActorVoList = movieService.getActorByMid(mid);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询电影详情成功！",
                    new MovieDetailVo(movieShowVo, movieActorVoList));
        }
        throw new IllegalArgumentException("电影id为空！");
    }

    @RequestMapping("/getCommentByMid")
    private RespEntity<Page<MovieCommentShowVo>> getCommentByMid(@RequestBody MovieCommentQueryDto queryDto) {
        Integer mid = queryDto.getMid();
        System.out.println("电影的ID为----------"+mid);
        Integer pageNo = queryDto.getPageNo();
        System.out.println("电影的页码为----------"+pageNo);
        Integer pageSize = queryDto.getPageSize();
        System.out.println("页面大小为----------"+pageSize);
        String orderByCondition = queryDto.getOrderByCondition();
        System.out.println("orderByCondition的值为"+orderByCondition);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (mid != null && !StringUtils.isEmpty(orderByCondition)) {
            Page<MovieCommentShowVo> movieCommentShowVoPage = movieCommentService.selectCommentByMid(mid, pageNo,
                pageSize, orderByCondition);
            System.out.println(">>> movieCommentShowVoPage:" + movieCommentShowVoPage);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询电影评论成功！", movieCommentShowVoPage);
        }
        throw new IllegalArgumentException("电影id或者排序规则为空！");
    }

    private byte[] getRequestBodyBytes(HttpServletRequest request) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try (InputStream inputStream = request.getInputStream()) {
            byte[] data = new byte[1024];
            int read;
            while ((read = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, read);
            }
        }
        return buffer.toByteArray();
    }
}
